package com.example.smartcitydemo.ui.smartbus.fragment

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.databinding.FragmentSmartBusThirdBinding
import com.example.smartcitydemo.viewModel.person.PersonViewModel
import com.example.smartcitydemo.viewModel.smartBus.SmartBusViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder

/**
 * @PACKAGE_NAME: com.example.smartcity_jscs.ui.smartbus.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/8/25 22:20
 * @Email: 1919796283@qq.com
 */
class SmartBusThirdFragment :
    BaseFragment<FragmentSmartBusThirdBinding>(R.layout.fragment_smart_bus_third) {
    private val personViewModel: PersonViewModel by activityViewModels()
    private val smartBusViewModel: SmartBusViewModel by activityViewModels()
    override fun addObservers(myViewBinding: FragmentSmartBusThirdBinding) {
        personViewModel.personInfo.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                myViewBinding.apply {
                    tvUserName.text = it.data!!.nickName
                    tvPhoneNum.text = it.data.phonenumber
                }
            }
        }
        smartBusViewModel.choiceItemData.observe(viewLifecycleOwner) {
            myViewBinding.apply {
                tvSmartBusStart.text = it.childData.first().name
                tvSmartBusEnd.text = it.childData.last().name

            }
        }
        smartBusViewModel.startStationData.observe(viewLifecycleOwner) {
            myViewBinding.apply {
                tvGetStartStation.text = it
            }
        }
        smartBusViewModel.endStationData.observe(viewLifecycleOwner) {
            myViewBinding.apply {
                tvGetOffStation.text = it
            }
        }
        smartBusViewModel.startStationList.observe(viewLifecycleOwner){

        }
        smartBusViewModel.endStationList.observe(viewLifecycleOwner){

        }
    }

    override fun initView(
        myViewBinding: FragmentSmartBusThirdBinding,
        savedInstanceState: Bundle?
    ) {
        myViewBinding.apply {
            toolbarSmartBusThird.tvContent.text = "站点信息"
            typeBarBusThirdFirst.typeBarText.text = "个人信息"
            typeBarThirdSecond.typeBarText.text = "站点选择"
            smartBusThirdBottomBar.btnSmartBusNext.setOnClickListener {
                this@SmartBusThirdFragment.findNavController()
                    .navigate(R.id.action_navigation_smart_bus_third_to_navigation_smart_bus_fourth)
            }
            smartBusThirdBottomBar.btnSmartBusPrevious.setOnClickListener {
                this@SmartBusThirdFragment.findNavController().navigateUp()
            }
            btnSmsrtStartChoice.setOnClickListener {
                MaterialAlertDialogBuilder(myContext).apply {
                    setTitle("选择上车点")
                    val array1: Array<String> = smartBusViewModel.startStationList.value?.toTypedArray()?: arrayOf()
                    setItems(array1){
                        dialog,which ->
                        val textString = array1[which]
                        tvGetStartStation.text=textString
                        btnSmartBusEndChoice.isEnabled=true
                        smartBusViewModel.putStartStation(textString)
                    }
                    show()
                }
            }
            btnSmartBusEndChoice.setOnClickListener {
                MaterialAlertDialogBuilder(myContext).apply {
                    setTitle("选择下车点")
                    val array = smartBusViewModel.endStationList.value!!.toTypedArray()
                    setItems(array) { dialog, which ->
                        val itemText = array[which]
                        tvGetOffStation.text = itemText
                        smartBusViewModel.putEndStation(itemText)
                    }
                    show()
                }
            }
            smartBusThirdBottomBar.btnSmartBusPrevious.setOnClickListener {
                this@SmartBusThirdFragment.findNavController().navigateUp()
            }
            smartBusThirdBottomBar.btnSmartBusNext.setOnClickListener {
                this@SmartBusThirdFragment.findNavController().navigate(R.id.action_navigation_smart_bus_third_to_navigation_smart_bus_fourth)
            }

        }
    }

    override fun initData() {
        personViewModel.getPersonInfo()

    }
}